####################################
# RDPLOTS
####################################

rm(list=ls())

#sink("~/Dropbox/Crime Chile/11_replication/14_RD_figures_fewer_bins_crime.txt")

library(Hmisc)
library(ggplot2)
library(stargazer)
library(foreign)
library(rdrobust)
library(rdd)
library(readstata13)

################
# Prepare data 
################

# reada data
d = read.dta13("~/Dropbox/Crime Chile/11_replication/local_crime_data_chile_2022january.dta")   
names(d)

############################
# RDplots
############################

# nbis = 50

# Homicides
summary(rdrobust(d$homicide_rate_s,d$margin,cluster = d$cluster,all=TRUE))
cairo_pdf(file="~/Dropbox/Crime Chile/11_replication/figureA17a.pdf", 
          width=7, 
          height=7)
rdplot(y = d$homicide_rate_s, x = d$margin, h= 0.162, nbins = 50, subset = -0.162 <= d$margin & d$margin <= 0.162,
        binselect="esmv", kernel="triangular", col.lines = "black", col.dots = "black", p=1, y.lim = c(-1, 1), title = "                                       Robust CI: [-0.440 , 0.183]",
        y.label = "Homicide", x.label = "Margin of victory")
dev.off() 

# Rapes
summary(rdrobust(d$rape_rate_s,d$margin,cluster = d$cluster,all=TRUE))
cairo_pdf(file="~/Dropbox/Crime Chile/11_replication/figureA17b.pdf", 
          width=7, 
          height=7)
(rdplot(y = d$rape_rate_s, x = d$margin,  h= 0.155, nbins = 50, subset = -0.155 <= d$margin & d$margin <= 0.155, 
        binselect="esmv", kernel="triangular", col.lines = "black", col.dots = "black", p=1, y.lim = c(-1, 1), title = "                                       Robust CI: [-0.284 , 0.131]",
        y.label = "Rape", x.label = "Margin of victory"))
dev.off()

# Assault
summary(rdrobust(d$assault_rate_s,d$margin,cluster = d$cluster,all=TRUE))
cairo_pdf(file="~/Dropbox/Crime Chile/11_replication/figureA17c.pdf", 
          width=7, 
          height=7)
(rdplot(y = d$assault_rate_s, x = d$margin,  h= 0.198, nbins = 50, subset = -0.198 <= d$margin & d$margin <= 0.198, 
        binselect="esmv", kernel="triangular", col.lines = "black", col.dots = "black", p=1, y.lim = c(-1, 1), title = "                                       Robust CI: [-0.305 , 0.157]",
        y.label = "Assault", x.label = "Margin of victory"))
dev.off()

# Theft
summary(rdrobust(d$theft_rate_s,d$margin,cluster = d$cluster,all=TRUE))
cairo_pdf(file="~/Dropbox/Crime Chile/11_replication/figureA18a.pdf", 
          width=7, 
          height=7)
(rdplot(y = d$theft_rate_s, x = d$margin,  h= 0.096, nbins = 50, subset = -0.096 <= d$margin & d$margin <= 0.096, 
        binselect="esmv", kernel="triangular", col.lines = "black", col.dots = "black", p=1, y.lim = c(-1, 1), title = "                                       Robust CI: [-0.963 , -0.290]",
        y.label = "Theft", x.label = "Margin of victory"))
dev.off()

# Robbery
summary(rdrobust(d$robbery_rate_s,d$margin,cluster = d$cluster,all=TRUE))
cairo_pdf(file="~/Dropbox/Crime Chile/11_replication/figureA18b.pdf", 
          width=7, 
          height=7)
(rdplot(y = d$robbery_rate_s, x = d$margin,  h= 0.106, nbins = 50, subset = -0.106 <= d$margin & d$margin <= 0.106, 
        binselect="esmv", kernel="triangular", col.lines = "black", col.dots = "black", p=1, y.lim = c(-1, 1), title = "                                       Robust CI: [-0.824 , -0.079]",
        y.label = "Robbery", x.label = "Margin of victory"))
dev.off()

# Robbery by surprise
summary(rdrobust(d$robbery_surprise_rate_s,d$margin,cluster = d$cluster,all=TRUE))
cairo_pdf(file="~/Dropbox/Crime Chile/11_replication/figureA18c.pdf", 
          width=7, 
          height=7)
(rdplot(y = d$robbery_surprise_rate_s, x = d$margin,  h= 0.110, nbins = 50, subset = -0.110 <= d$margin & d$margin <= 0.110, 
        binselect="esmv", kernel="triangular", col.lines = "black", col.dots = "black", p=1, y.lim = c(-1, 1), title = "                                       Robust CI: [-0.823 , -0.074]",
        y.label = "Robbery by surprise", x.label = "Margin of victory"))
dev.off()

sink()
